<template>
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top" >
        <div class="container">
            <router-link class="navbar-brand" :to="{name: 'TeacherInvoice'}"
                v-if="$store.state.user.identity == 'teacher'"
            >OCR</router-link>
            <router-link class="navbar-brand" :to="{name: 'InvoiceRecord'}"
                v-if="$store.state.user.identity == 'student' || $store.state.user.identity == ''"
            >OCR</router-link>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarText">
                <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                    <!-- <li class="nav-item">
                        <router-link :class="route_name == 'home' ? 'nav-link active' : 'nav-link'" :to="{name: 'home'}">首页</router-link>
                    </li> -->
                    <li class="nav-item">
                        <router-link
                            :class="route_name == 'TeacherInvoice' || route_name == 'StudentInvoice' ? 'nav-link active' : 'nav-link'" :to="{name: 'TeacherInvoice'}"
                            v-if="$store.state.user.identity == 'teacher'"
                        >发票列表</router-link>
                    </li>
                    <li class="nav-item">
                        <router-link 
                            :class="route_name == 'InvoiceRecord' ? 'nav-link active' : 'nav-link'" :to="{name: 'InvoiceRecord'}"
                            v-if="$store.state.user.identity == 'student' || $store.state.user.identity == ''"
                        >发票列表</router-link>
                    </li>
                    <li class="nav-item">
                        <router-link :class="route_name == 'InvoiceOCR' ? 'nav-link active' : 'nav-link'" :to="{name: 'InvoiceOCR'}">发票识别</router-link>
                    </li>
                    <li class="nav-item">
                        <router-link :class="route_name == 'InvoiceSearch' ? 'nav-link active' : 'nav-link'" :to="{name: 'InvoiceSearch'}">发票检索</router-link>
                    </li>
                </ul>
                <ul class="navbar-nav" v-if="$store.state.user.is_login">
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                            <img class="user-photo" :src="$store.state.user.photo">
                        </a>
                        <ul class="dropdown-menu">
                            <li><router-link class="dropdown-item" :to="{name: 'userprofile'}">我的空间</router-link></li>
                            <li><hr class="dropdown-divider"></li>
                            <li><a class="dropdown-item" style="cursor: pointer;" @click="logout">退出</a></li>
                        </ul>
                    </li>
                </ul>
                <ul class="navbar-nav" v-else-if="!$store.state.user.pulling_info">
                    <li class="nav-item">
                        <router-link :class="route_name == 'login' ? 'nav-link active' : 'nav-link'" :to="{name: 'login'}" role="button">
                            登录
                        </router-link>
                    </li>
                    <li class="nav-item">
                        <router-link :class="route_name == 'register' ? 'nav-link active' : 'nav-link'" :to="{name: 'register'}" role="button">
                            注册
                        </router-link>
                    </li>
                </ul>
            </div>
        </div>
    </nav>
</template>

<script setup>
import { useRoute } from 'vue-router';
import { computed } from 'vue';
import { useStore } from 'vuex';

const store = useStore();
const route = useRoute();
let route_name = computed(() => route.name);

const logout = () => {
    store.commit('logout');   // 调用mutations里的API用commit
};

</script>

<style scoped>
.navbar {
    height: 65px;  /* 设置固定的导航栏高度 */
    display: flex;
    align-items: center;  /* 垂直居中内容 */
}

.user-photo {
    width: 40px; /* 增大头像宽度 */
    height: 40px; /* 增大头像高度 */
    margin-right: 5px;
    border-radius: 50%;
    object-fit: cover;  /* 保持头像比例 */
    vertical-align: middle;  /* 确保头像居中 */
}
</style>